package com.cloudsigns.factorymanager2.project.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import android.widget.EditText;
import android.widget.ImageView;

import android.widget.RelativeLayout;


import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DpUtil;

/**
 * Created by tony on 2017/12/16.
 */

public class TitleDialog extends Dialog {

    private Context mContext;
    private View dialogView;

    public TitleDialog(Context context) {
        super(context, R.style.TitleDialogTheme);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogView);
    }

    /**
     * 创建项目管理页面 头部对话框
     *
     * @param onButtonClickListener
     */
    public void createProjectManagerTitleDialog(final OnButtonClickListener onButtonClickListener) {
        dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_title, null);

        initProjectManagerViewHolder(onButtonClickListener);

        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
        localLayoutParams.x = 0;
        int y = DpUtil.dip2px(mContext, 40);
        localLayoutParams.y = y;
        localLayoutParams.gravity = Gravity.TOP;
        dialogView.setMinimumWidth(10000);

        onWindowAttributesChanged(localLayoutParams);
        setCanceledOnTouchOutside(true);
        setCancelable(true);

    }

    private void initProjectManagerViewHolder(final OnButtonClickListener onButtonClickListener) {

        final ProjectManagerViewHolder holder = new ProjectManagerViewHolder();
        holder.edt_project_name = dialogView.findViewById(R.id.edt_project_name);
        holder.edt_engineering_name = dialogView.findViewById(R.id.edt_engineering_name);
        holder.img_project_name = dialogView.findViewById(R.id.img_project_name);
        holder.img_engineering_name = dialogView.findViewById(R.id.img_engineering_name);
        holder.btn_search = dialogView.findViewById(R.id.btn_search);


        holder.btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String projectName = holder.edt_project_name.getText().toString().trim();
                String engineeringName = holder.edt_engineering_name.getText().toString().trim();

                Bundle bundle = new Bundle();
                bundle.putString(ConstantUtil.PROJECT_NAME, projectName);
                bundle.putString(ConstantUtil.ENGINEERING_NAME, engineeringName);


                if (onButtonClickListener != null) {
                    onButtonClickListener.onButtonClick(bundle);
                }
                dismiss();
            }
        });
    }


    public interface OnButtonClickListener {
        void onButtonClick(Bundle bundle);
    }

    class ProjectManagerViewHolder {
        EditText edt_project_name;
        ImageView img_project_name;
        EditText edt_engineering_name;
        ImageView img_engineering_name;
        RelativeLayout btn_search;
    }

}
